package com.myjava.managesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myjava.managesystem.common.CustomException;
import com.myjava.managesystem.dto.DishDto;
import com.myjava.managesystem.dto.SetmealDish;
import com.myjava.managesystem.entity.Dish;
import com.myjava.managesystem.entity.DishFlavor;
import com.myjava.managesystem.entity.Setmeal;
import com.myjava.managesystem.mapper.DishMapper;
import com.myjava.managesystem.service.DishFlavorService;
import com.myjava.managesystem.service.DishService;
import com.myjava.managesystem.service.SetmealDishService;
import com.myjava.managesystem.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    SetmealDishService setmealDishService;

    @Lazy
    @Autowired
    SetmealService setmealService;

    @Override
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        for(DishFlavor d:flavors){
            d.setDishId(dishId);
        }
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        DishDto dishDto = new DishDto();
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getId,id);
        Dish dish = this.getOne(wrapper);
        BeanUtils.copyProperties(dish,dishDto);
        LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(wrapper1);
        dishDto.setFlavors(list);
        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        for(DishFlavor d:flavors){
            d.setDishId(dishId);
        }
        dishFlavorService.updateBatchById(flavors);
    }

    @Override
    public void updateStatusWithSetmeal(int st, List<Long> ids) {
        if(st<1){
            LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.in(SetmealDish::getDishId,ids);
            List<SetmealDish> setmealDishList = setmealDishService.list(wrapper1);
            if(!setmealDishList.isEmpty()){
                Set<Long> setmealIds = new HashSet<>();
                for(SetmealDish setmealDish:setmealDishList){
                    setmealIds.add(setmealDish.getSetmealId());
                }
                LambdaQueryWrapper<Setmeal> wrapper2 = new LambdaQueryWrapper<>();
                wrapper2.in(Setmeal::getId,setmealIds);
                wrapper2.eq(Setmeal::getStatus,1);
                if(setmealService.count(wrapper2)>0){
                    throw new CustomException("有在售套餐关联该菜品，无法停售");
                }
            }

        }
        LambdaQueryWrapper<Dish> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.in(Dish::getId,ids);
        List<Dish> list = this.list(wrapper3);
        for (Dish dish:list){
            dish.setStatus(st);
        }
        this.updateBatchById(list);
    }

    @Override
    public void deleteWithSetmeal(List<Long> ids) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);
        wrapper.eq(Dish::getStatus,1);
        if(this.count(wrapper)>0){
            throw new CustomException("菜品正在售卖，无法删除");
        }
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getDishId,ids);
        if(setmealDishService.count(wrapper1)>0){
            throw new CustomException("有套餐关联当前菜品，无法删除");
        }
        this.removeByIds(ids);
    }
}
